package net.huashitong.user.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.user.service.LoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/app/loginLog")
public class LoginLogController extends JsonBaseController {
    @Autowired
    private LoginLogService loginLogService;

    @Override
    protected String getView(String s) {
        return null;
    }

    @RequestMapping("/clickCount")
    @ResponseBody
    public Message getClickCount() {
        Map<String, Integer> result = loginLogService.queryDeptLoginCount();
        Map<String, Integer> other = loginLogService.queryLoginCount();
        result.putAll(other);
        Map<String, Integer> clickCount = new LinkedHashMap<String, Integer>();
        String[] branchs = {
                "永泰县自然资源和规划局",
                "永泰县住房和城乡建设局",
                "永泰县发展改革局",
                "永泰县生态环境局",
                "永泰县水利局",
                "永泰县林业局",
                "永泰县财政局",
                "永泰县公安消防大队",
                "永泰县人民防空办公室",
                "永泰县人力资源和社会保障局",
                "县重点工作攻坚指挥室",
                "业主单位"
        };
        List<String> branchList = Arrays.asList(branchs);
        for (Map.Entry<String, Integer> entry : result.entrySet()) {
            String key = entry.getKey();
            switch (key){
                case "永泰县自然资源和规划局": key = "国土局"; break;
                case "永泰县住房和城乡建设局": key = "住建局"; break;
                case "永泰县发展改革局": key = "发改局"; break;
                case "永泰县生态环境局": key = "环保局"; break;
                case "永泰县水利局": key = "水利局"; break;
                case "永泰县林业局": key = "林业局"; break;
                case "永泰县财政局": key = "财政局"; break;
                case "永泰县公安消防大队": key = "消防大队"; break;
                case "永泰县人民防空办公室": key = "人防办"; break;
                case "永泰县人力资源和社会保障局": key = "人社局"; break;
                case "县重点工作攻坚指挥室": key = "攻坚办"; break;
                case "业主单位": key = "业主单位"; break;
                default: key = "";break;
            }

            if (StringUtils.isNotBlank(key)) {
                clickCount.put(key, entry.getValue());
            }
        }
        for (String branch : branchList) {
            if (ObjectUtils.isBlank(result.get(branch))) {
                clickCount.put(branch, 0);
            }
        }
        // 升序比较器
        Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        };

        // map转换成list进行排序
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(clickCount.entrySet());

        // 排序
        Collections.sort(list, valueComparator);
        return coverMessage("200", "部门点击率获取成功", list);
    }


    @RequestMapping("/clickUserCount")
    @ResponseBody
    public Message getClickUserCount() {
        Map<String, Integer> result = loginLogService.queryUserLoginCount();
        Map<String, Integer> other = loginLogService.queryLoginCount();
        result.putAll(other);
        Map<String, Integer> clickCount = new LinkedHashMap<String, Integer>();
        /*String[] branchs = {"国土局", "住建局", "发改局", "环保局", "水利局", "林业局", "财政局",  "消防大队", "人防办",
                "人社局", "业主单位", "攻坚办"};*/
        //数字峰会
        String[] branchs = {"国土局", "住建局", "发改局", "环保局", "水利局", "林业局", "财政局",  "消防大队", "人防办",
                "人社局", "攻坚办"};
        List<String> branchList = Arrays.asList(branchs);
        for (Map.Entry<String, Integer> entry : result.entrySet()) {
            if (branchList.contains(entry.getKey())) {
                clickCount.put(entry.getKey(), entry.getValue());
            }
        }
        for (String branch : branchList) {
            if (ObjectUtils.isBlank(result.get(branch))) {
                clickCount.put(branch, 0);
            }
        }
        // 升序比较器
        Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        };
        // map转换成list进行排序
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(clickCount.entrySet());
        // 排序
        Collections.sort(list, valueComparator);
        return coverMessage("200", "部门点击率获取成功", list);
    }
}
